<?php
if ( defined('ABSPATH') )
	include_once ABSPATH . 'config.php';
else
    include_once 'config.php';

include_once ABSPATH."base/curl.php";
include_once ABSPATH."base/people.php";

// how many rows to show per page
$rowsPerPage = 20;

// by default we show first page
$pageNum = 1;

// if $_GET['page'] defined, use it as page number
if(isset($_POST['page']))
{
	$pageNum = $_POST['page'];
}

// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;


$directory = "";

if(isset($_POST["directory"]))
{
	$directory = $_POST["directory"];
}
else
{
	$directory = HOME."directory/";
}

if(isset($_COOKIE["ombid"]))
{
	$id = $_COOKIE["ombid"];
}
else
{
	header("Location: index.php");
}

if(isset($_POST["btnSave"]))
{
	foreach($_POST["chkContact"] as $contact)
	{
			$contact_id = Person::IDFromAddress($contact);
			if(strlen($contact_id) == 0)
			{
				$person = new Person();
				$contact_id = $person->CreateExt($contact);
				
			}
			$person = new Person($id);
			$person->Connect($contact_id);
	}
}



include_once THEME_PATH."header.php";

?>
<div id="page">
<?php include_once "header.php"; ?>
<div id="content" class="widecolumn">
<input type="hidden" id="id">
<a href="index.php">Home</a>
<ul class="layoutA">
<li>
<form method="post">
<input type="hidden" id="pageNum" name="page" value="<?php echo $pageNum ?>">
<ul class="cnxnsrch-instr"><li>Enter a directory address below, or use the default directory.</li><br/>
<li>Enter search criteria or leave blank for all.  Click the <strong>Search</strong> button.</li><br/>
<li>Click the checkbox next to users you'd like to connect with, and then click the <strong>Connect</strong> button.</li></ul>
<p><input type="text" id="directory" name="directory" value="<?php echo $directory ?>" class="link"></p>
<p><input type="text" name="txtSearch" value="<?php echo $_POST["txtSearch"] ?>" /><input type="submit" name="btnSearch" value="Search"/></p>
<input type="submit" name="btnSave" value="Connect">
<div id="divList">
<?php

//Connect to Directory Server
//SearchText, PageNum, MaxResults
//Reply Unique Address, Name
//Lookup Addresses, Filter if already Connected
//Selected, If local, just connect
//If not local, Request details, insert, then connect


if(isset($_POST["btnSearch"]) || isset($_POST["page"]))
{
	$curl = new cURL(FALSE);

	$url = $directory."?q=".$_POST["txtSearch"]."&page=".$pageNum."&count=".$rowsPerPage."&nocache=".urlencode(Date("Y-m-d H:i:s"));
	$users = $curl->get($url);

	$dom = new DomDocument();
	$dom->loadXML($users);
	
	$nodes = $dom->getElementsByTagName("user");
	$users = $dom->childNodes->item(0);
	$numRows = $users->getAttribute("total");

	foreach($nodes as $user )
	{
		$contact_id = '';
		$address='';
		$name = '';
		$screen_name='';
		foreach($user->childNodes as $row )
		{
			switch($row->nodeName)
			{
				case "name":
				$name = $row->nodeValue;
				break;
				case "screen_name":
				$screen_name = $row->nodeValue;
				break;
				case "address":
				$address = $row->nodeValue;
				if(strlen($address) > 0)
				{
					$contact_id = Person::IDFromAddress($address);
				}
				break;		
				
			}
			
		}
	    $connected = false;
	
	    if(strlen($contact_id) > 0)
	    {
			$person = new Person($id);
			if($person->ConnectionGet($contact_id))
			{
				$connected = true;
			}
	    }
	
		if(!$connected)
		{
			$alt = $alt == "cnxnsrch-row" ? "":"cnxnsrch-row";
			echo '<p class="'.$alt.'"><input name="chkContact[]" value="'.$address.'" type="checkbox"><a href="javascript:void(0)" onclick="Profile(\''.$address.'\',DOM(\'divDetail\'))" title="'.$screen_name.'">'.$name.'</a></p>';	
		}	
	}

	// how many pages we have when using paging?
	$maxPage = ceil($numRows/$rowsPerPage);
	
	
	$self = $_SERVER['PHP_SELF'];
	
	// creating 'previous' and 'next' link
	// plus 'first page' and 'last page' link
	
	// print 'previous' link only if we're not
	// on page one
	if ($pageNum > 1)
	{
		$page = $pageNum - 1;
		$prev = " <a href='javascript:void(0)' onclick='return PeopleListGet($page);'><</a> ";
		$first = " <a href='javascript:void(0)' onclick='return PeopleListGet(1);'><<</a> | ";
	} 
	else
	{
		$prev  = ' < ';       // we're on page one, don't enable 'previous' link
		$first = ' << | '; // nor 'first page' link
	}
	
	// print 'next' link only if we're not
	// on the last page
	if ($pageNum < $maxPage)
	{
		$page = $pageNum + 1;
		$next = " <a href='javascript:void(0)' onclick='return PeopleListGet($page);'>></a> | ";
		$last = " <a href='javascript:void(0)' onclick='return PeopleListGet($maxPage);'>>></a> ";
	} 
	else
	{
		$next = ' > | ';      // we're on the last page, don't enable 'next' link
		$last = ' >> '; // nor 'last page' link
	}
	
	// print the page navigation link
	echo $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages " . $next . $last;
	
	
}

?>
</div>
</form>
</li>
<li>
<div id="divDetail"></div>
</li>
</ul>
</div>
<?php include_once THEME_PATH."footer.php";?>
